import formJs from "./regexDetail.js"; //新增弹框配置项
import publicForm from "@/components/public/up-form.vue"; //弹框form
import FormLyout from '@/components/commonPublic/fromLayout.vue'
import actions from "@/actions";

import {
    saveOutsourcedReport, //保存
    getOutsourcedReportByFid, //详情
} from "@/api/request.js";
export default {

    data() {
        return {
            visible: "",
            loading: false,
            title: null,
            isLock: false,
            positionData: [],
            positionData1: [],
            selected: [], //多选人
            btnStatus: [],
            positionObj: [],
            dialogHeight: null,
            activeIndex: "1",
            formObj: formJs.formObj,
            currntData: null,
            dataInfo: null,
            startDates: null,
            treeData: [], //材料种类
            formData: {
                fId: "", //必传
                userId: "", //当前登录人id
                userName: "", //当前登录人姓名
                recordfiles: [], //删除文件id集合
                fReportnumber: "", //报告编号
                fReportdates: this.common.day(0), //报告日期
                fReportfileurl: "", //报告文件
                fReportstatus: "", //报告状态
                fReportresult: "", //报告判定
                fNonresult: "", //不合格参数及结果
                fRemark: "" ,//报告备注
                fChecktype:"",
            },
            formDatafileList: [], //工作计划资料
            btnShow: true,
            currentDetail: null,
            selectPostionObj: [],
            types: [],
            // 材料弹框
            materialRow: {},

            fCompanynames: null,
            fCompanyids: null,
            fFileurllist: [], //附件
            primaryInfo:null,

        };
    },
    components: {
        publicForm,
        FormLyout
    },

    
    mounted() {
        this.primaryInfo = JSON.parse(sessionStorage.getItem("GxEmbedded_primaryInfo"))
        this.btnShow = true;
        actions.onGlobalStateChange((state, prev) => {
            var data = prev.currentUser.projects;
            this.userInfo = prev.currentUser



        }, true);
        if (this.primaryInfo.status == 1) { //编辑
            this.formData.fId = this.primaryInfo.data.fId;//按钮 
            this.getDetail({
                fId: this.primaryInfo.data.fId
            })
            this.formObj.map((item) => {
                item.disabled = false;
            });
            this.formObj[0].disabled = true;
            this.formObj[1].disabled = true;
            this.formObj[2].disabled = true;
            this.formObj[3].disabled = true;
            this.formObj[4].disabled = true;
            this.formObj[5].disabled = true;
            this.formObj[9].uploadSelect = true
        } else if (this.primaryInfo.status == 7) { //查看
            this.getDetail({
                fId: this.primaryInfo.data.fId
            })
            this.formObj.map((item) => {
                item.disabled = false;
            });
            this.btnShow = false
            this.formObj.map((item) => {
                item.disabled = true;
            });
            this.formObj[9].uploadSelect = false
        }
    },
    methods: {
      
        
        selectChange(data, a, b) {
            console.log(data, a, b);
            if (b == "fReportresult") { //报告判定
                if (data == '合格') {
                    this.formObj[10].disabled = true;
                    this.formObj[10].rules[0].required = false;
                } else if (data == '不合格') {
                    this.formObj[10].disabled = false;
                    this.formObj[10].rules[0].required = true;
                }
            }
        },

        // 刪除文件
        delteFile(file) {
            for (let index = 0; index < this.fFileurllist.length; index++) {
                if (file.uid == this.fFileurllist[index].uid) {
                    if (this.fFileurllist[index].atta == undefined) {
                        this.formData.recordfiles.push({
                            fId: this.fFileurllist[index].fId,
                        });
                    } else {
                        this.formData.recordfiles.push({
                            fId: this.fFileurllist[index].atta.fId,
                        });
                    }
                    this.fFileurllist.splice(index, 1);
                }
            }
        },
        // 详情
        getDetail(obj) {
            this.loading = true;
            getOutsourcedReportByFid(obj).then((res) => {
                if (res.errcode == 0) {
                    this.loading = false;
                    var data = res.data.yjQmpClOutsourcedtesting
                    this.formData = data
                    this.formData.fReportfileurl = data.fReportfileurl
                    if (data.fReportdates == null || data.fReportdates == "") {
                        this.formData.fReportdates = this.common.day(0)
                    } else {
                        this.formData.recordfiles = []
                        this.fFileurllist = res.data.fReportfileurllist;
                        
                       
                        if(this.primaryInfo.status==7){
                            this.btnShow = false;
                            this.formObj[10].disabled = true;
                            this.formObj[10].rules[0].required = false;
                        }else {
                            this.btnShow = true;
                            if (data.fReportresult == '合格') {
                                this.formObj[10].disabled = true;
                                this.formObj[10].rules[0].required = false;
                            } else if (data.fReportresult == '不合格') {
                                this.formObj[10].disabled = false;
                                this.formObj[10].rules[0].required = true;
                            }
                        }
                    }
                } else {
                    this.loading = false;
                    this.$message.error({
                        content: res.msg,
                        duration: 1000
                    })
                }
            });
        },
        yj_saveForm() {
            this.formData.userId = this.userInfo.id; //当前登录人id
            this.formData.userName = this.userInfo.userName; //当前登录人名称
            if (this.$refs.publicForm.submitForm()) {
                this.saveForm(this.formData);
            } else {
                console.log("error");
            }
        },
        // 保存
        saveForm(obj) {
            var data = {
                fId: obj.fId, //必传
                userId: obj.userId, //当前登录人id
                userName: obj.userName, //当前登录人姓名
                recordfiles: obj.recordfiles, //删除文件id集合
                fReportnumber: obj.fReportnumber, //报告编号
                fReportdates: obj.fReportdates, //报告日期
                fReportfileurl: obj.fReportfileurl, //报告文件
                fReportstatus: obj.fReportstatus, //报告状态
                fReportresult: obj.fReportresult, //报告判定
                fNonresult: obj.fNonresult, //不合格参数及结果
                fRemark: obj.fRemark, //报告备注
                fCompanyid:obj.fCompanyid,
                fChecktype:obj.fChecktype,
            }
            this.loading = true;

            this.isLock = true
            saveOutsourcedReport(data)
                .then((res) => {
                    if (res.errcode == 0) {
                        this.loading = false;
                        this.$router.push({
                            path: "/outsourcedManage/qualificationReport",
        
                        });
                        window.sessionStorage.removeItem("GxEmbedded_primaryInfo");
                        this.$message.success({
                            content: "新增成功",
                            duration: 1000
                        })
                        this.isLock = false
                    } else {
                        this.loading = false;
                        this.isLock = false
                        this.$message.error({
                            content: res.msg,
                            duration: 1000
                        })
                        console.log("接口报错");
                    }
                })
        },
    },
};